//
//  AnswerViewController.m
//  AssistantDriving
//
//  Created by dcr on 2017/8/28.
//  Copyright © 2017年 邓兆太. All rights reserved.
//

#import "AnswerViewController.h"
#import "AnswerScrollView.h"
#import "AnswerModel.h"
#import "SelectModelView.h"
#import "SheetView.h"
#import "QuestionCollectManager.h"
#import "ImageButton.h"

@interface AnswerViewController ()<SheetViewDelegate, AnswerScrollViewDelegate>
{
    AnswerScrollView *_answerScrollview;
    SelectModelView *_modelView;
    SheetView *_sheetView;
    int _count;
}
@end

@implementation AnswerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    [self createData];
    [self.view addSubview:_answerScrollview];
    [self createToolBar];
    [self createModelView];
    [self createSheetView];
}

- (void)createData{
    switch (_model) {
        case chapterPractice:{
            NSMutableArray *array = [[NSMutableArray alloc]init];
            //读取答案表
            NSArray *arr = [DataManager getData:answer];
            for(int i = 0; i < arr.count; i++){
                AnswerModel *model = arr[i];
                //根据用户选中的Item值，对比pid一致的行添加
                if([model.pid intValue] == _number+1){
                    [array addObject:model];
                }
            }
            _count = (int)array.count;
            _answerScrollview = [[AnswerScrollView alloc]initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight-124) andDataArray:array];
        }
            break;
        case orderPractice:{
            NSArray *array = [DataManager getData:answer];
            _count = (int)array.count;
            _answerScrollview = [[AnswerScrollView alloc]initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight-124) andDataArray:array];
        }
            break;
        case randomPractice:{
            NSMutableArray *temArr = [[NSMutableArray alloc] init];
            NSArray *array = [DataManager getData:answer];
            NSMutableArray *dataArray = [[NSMutableArray alloc] init];
            [temArr addObjectsFromArray:array];
            for(int i = 0; i < array.count; i++){
                int index = arc4random()%(temArr.count);
                [dataArray addObject:temArr[index]];
                [temArr removeObjectAtIndex:index];
            }
            _count = (int)dataArray.count;
            _answerScrollview = [[AnswerScrollView alloc]initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight-124) andDataArray:dataArray];
        }
            
            break;
        case specialPractice:{
            NSMutableArray *array = [[NSMutableArray alloc]init];
            //读取答案表
            NSArray *arr = [DataManager getData:answer];
            for(int i = 0; i < arr.count; i++){
                AnswerModel *model = arr[i];
                //根据用户选中的Item值，对比pid一致的行添加
                if([model.sid isEqualToString:_mtype]){
                    [array addObject:model];
                }
            }
            _count = (int)array.count;
            _answerScrollview = [[AnswerScrollView alloc]initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight-124) andDataArray:array];
            
        }
            break;
        case myWrong:{  //读取错题
            NSMutableArray *array = [[NSMutableArray alloc]init];
            //读取答案表
            NSArray *answers = [DataManager getData:answer];
            NSArray *wrongArr = [QuestionCollectManager getWrongQuestion];
            for(AnswerModel *model in answers){
                for(NSString *mid in wrongArr){
                    if([mid isEqualToString:model.mid]){
                        [array addObject:model];
                    }
                }
            }
#warning count小于3时有问题，由于AnswerScrollView最少会创建3个页面
            _count = (int)array.count;
            _answerScrollview = [[AnswerScrollView alloc]initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight-124) andDataArray:array];
        }
            break;
        case myFavorite:{  //读取收藏
            NSMutableArray *array = [[NSMutableArray alloc]init];
            //读取答案表
            NSArray *arr = [DataManager getData:answer];
            if(arr == nil || arr.count <= 0)
                break;
            NSArray *favoriteArr = [QuestionCollectManager getCollectQuestion];
            if(favoriteArr == nil || favoriteArr.count <= 0)
                break;
            for(AnswerModel *model in arr){
                for(NSString *mid in favoriteArr){
                    if([mid isEqualToString:model.mid]){
                        [array addObject:model];
                    }
                }
            }
            _count = (int)array.count;
            _answerScrollview = [[AnswerScrollView alloc]initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight-124) andDataArray:array];
        }
            break;
            
        default:
            break;
    }
    _answerScrollview.delegate = self;
}

//创建答案的表视图控件
- (void)createSheetView{
    CGRect rect = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-80);
    _sheetView = [[SheetView alloc] initWithFrame:rect withSuperView:self.view andQuesCount:_count];
    [self.view addSubview:_sheetView];
    _sheetView.delegate = self;
}

//创建答题模式控件
- (void)createModelView{
    _patternType = ExamPattern;
    _modelView = [[SelectModelView alloc]initWithFrame:self.view.frame andTouch:^(SelectModel model){
        if(model == 0){
            _patternType = ExamPattern;
            [self.navigationItem.rightBarButtonItem setTitle:@"答题模式"];
        }else{
            _patternType = AnswerPattern;
            [self.navigationItem.rightBarButtonItem setTitle:@"背题模式"];
            [self lookAnswer];
        }
    }];
    _modelView.alpha = 0;
    [self.view addSubview:_modelView];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"答题模式" style:UIBarButtonItemStylePlain target:self action:@selector(modelChange:)];
    self.navigationItem.rightBarButtonItem = item;
}

- (void)modelChange:(UIBarButtonItem *)item{
    if(_modelView.alpha == 1){
        [UIView animateWithDuration:0.2 animations:^{
            _modelView.alpha = 0;
        }];
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            _modelView.alpha = 1;
        }];
    }
}

//创建底部三个按钮
- (void)createToolBar{
    UIView *barView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-60-64, self.view.frame.size.width, 60)];
    barView.backgroundColor = [UIColor whiteColor];
    NSString *count = [NSString stringWithFormat:@"%d", _count];
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    [arr addObjectsFromArray:@[count, @"查看", @"收藏"]];
    
    for(int i = 0; i < arr.count; i++){
        ImageButton *btn = [ImageButton buttonWithType:UIButtonTypeSystem];
        btn.backgroundColor = [UIColor clearColor];
        
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d",16+i]] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d-2",16+i]] forState:UIControlStateHighlighted];
        btn.tag = 1000+i;
        [btn addTarget:self action:@selector(onClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn setTitleColor:GeneralColor1 forState:UIControlStateNormal];
        [btn.titleLabel setFont:WeekFont4];
        [btn verticalImageAndTitle:15.0];
        [barView addSubview:btn];
        CGFloat x = (MainScreenWidth/6.0) + (MainScreenWidth/3.0)*i - 20;
        [btn mas_makeConstraints:^(MASConstraintMaker *make){
            make.left.mas_equalTo(x);
            make.topMargin.mas_equalTo(5);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
    }
    [self.view addSubview:barView];
}

- (void)onClickButton:(UIButton *)sender{
    switch (sender.tag) {
        case 1000:
        {
            [UIView animateWithDuration:0.3 animations:^{
                _sheetView.frame = CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height-80);
                _sheetView.backView.alpha = 0.8;
            }];
        }
            break;
        case 1001:
        {     //查看答案
            [self lookAnswer];
        }
            break;
            
        case 1002:
        {    //收藏本题
            AnswerModel *model = _answerScrollview.datas[_answerScrollview.currentPage];
            [QuestionCollectManager addCollectQuestion:[model.mid intValue]];
        }
            break;
            
        default:
            break;
    }
}

- (void)lookAnswer{
    if([_answerScrollview.hadAnswerArray[_answerScrollview.currentPage] intValue ] != 0){
        return;
    }else{
        AnswerModel *mode = [_answerScrollview.datas objectAtIndex:_answerScrollview.currentPage];
        NSString *answer = mode.manswer;
        char an = [answer characterAtIndex:0];
        [_answerScrollview.hadAnswerArray replaceObjectAtIndex:_answerScrollview.currentPage withObject:[NSString stringWithFormat:@"%d", an-'A'+1]];
    }
    [_answerScrollview reloadData];
}

#pragma mark - delegate
-(void)SheetViewClick:(int)index{
    //点击题目的索引切换内容
    UIScrollView *scrollView = _answerScrollview->_scrollView;
    scrollView.contentOffset = CGPointMake((index)*scrollView.frame.size.width, 0);
    [scrollView.delegate scrollViewDidEndDecelerating:scrollView];
}

- (void)scrollViewDidEndDecelerating{
    if(_patternType == AnswerPattern)
        [self lookAnswer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
